﻿using System.Linq;
using System.Xml.Linq;
using ExcelAutomator.Configuration;
using NUnit.Framework;

namespace ExcelAutomator.Tests
{
	[TestFixture]
	public class DeserializationTestFixture
	{

		#region DeserializeExecutors
		[Test]
		public void DeserializeExecutors()
		{
			var cfg = ExcelAutomatorConfiguration.FromXml(TestHelper.GetStringResource("ExcelAutomator.Tests.Resources.SimpleExecutorSet.xml"));

			Assert.That(cfg.ExecutionSets.Count, Is.EqualTo(1));
			Assert.That(cfg.ExecutionSets.First().Executors.Count(), Is.EqualTo(3));

		}
		#endregion

		#region EnsureExecutorTypeWillBeDeserialized
		[Test]
		public void EnsureExecutorTypeWillBeDeserialized()
		{
			var cfg = ExcelAutomatorConfiguration.FromXml(TestHelper.GetStringResource("ExcelAutomator.Tests.Resources.SimpleExecutorSet.xml"));

			var executors = cfg.ExecutionSets.First().Executors;

			foreach (var executor in executors)
			{
				Assert.That(executor.Type, Is.EqualTo("range"));
			}
		}
		#endregion

		#region EnsureExecutorXmlConfigurationIsAvailable
		[Test]
		public void EnsureExecutorXmlConfigurationIsAvailable()
		{
			var cfg = ExcelAutomatorConfiguration.FromXml(TestHelper.GetStringResource("ExcelAutomator.Tests.Resources.SimpleExecutorSet.xml"));

			var executor = cfg.ExecutionSets.First().Executors.First();

			Assert.That(executor.XmlConfiguration, 
				Is.EqualTo(
				string.Format("<e:range action=\"copy\" sourceAddress=\"A1\" targetAddress=\"A2\" xmlns:e=\"{0}\" />", Constants.ExecutorExtensionsNamespace)));

		}
		#endregion

		#region DeserializeExecutionSetId
		[Test]
		public void DeserializeExecutionSetId()
		{
			var cfg = ExcelAutomatorConfiguration.FromXml(TestHelper.GetStringResource("ExcelAutomator.Tests.Resources.SimpleExecutorSet.xml"));

			Assert.That(cfg.ExecutionSets.First().Id, Is.EqualTo("My Execution Batch"));
		}
		#endregion

		#region DeserializeMultipleExecutionSets
		[Test]
		public void DeserializeMultipleExecutionSets()
		{
			var cfg = ExcelAutomatorConfiguration.FromXml(TestHelper.GetStringResource("ExcelAutomator.Tests.Resources.MultipleExecutorSets.xml"));

			Assert.That(cfg.ExecutionSets.Count, Is.EqualTo(2));
			Assert.That(cfg.ExecutionSets.First().Executors.Count, Is.EqualTo(3));
			Assert.That(cfg.ExecutionSets.ElementAt(1).Executors.Count, Is.EqualTo(2));

		}
		#endregion

		#region ExtendSchemaWithANewExecutorType
		[Test]
		public void ExtendSchemaWithANewExecutorType()
		{
			var schema = TestHelper.GetStringResource("ExcelAutomator.Tests.SchemaExtension.FoobarExecutor.xsd");
			var xml = TestHelper.GetStringResource("ExcelAutomator.Tests.SchemaExtension.FoobarExecutor.xml");
			var sut = ExcelAutomatorConfiguration.FromXml(xml, new[]{
					new SchemaContainer(
					Constants.ExecutorExtensionsNamespace, 
					schema)});

			Assert.That(sut.ExecutionSets.Count, Is.EqualTo(1));
			Assert.That(sut.ExecutionSets.First().Executors.First().Type, Is.EqualTo("foobar"));

			var xDoc = XDocument.Parse(sut.ExecutionSets.First().Executors.First().XmlConfiguration);

			var xElement = xDoc.Descendants().First();

			var fooAttribute = xElement.Attribute(XName.Get("foo"));
			var buzzAttribute = xElement.Attribute(XName.Get("buzz"));

      Assert.That(fooAttribute.Value, Is.EqualTo("A1"));
      Assert.That(buzzAttribute.Value, Is.EqualTo("A2"));
		}
		#endregion

	}
}
